<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\batch\kuaishou\schema;

use app\model\KsAdvertiserModel;
use app\model\mongo\MongoKsPicModel;
use app\model\mongo\MongoKsSchemaPicModel;
use app\model\mongo\MongoKsSchemaVideoModel;
use app\model\mongo\MongoKsUserVideoModel;
use app\service\batch\MediaInterface;
use app\service\batch\MediaService;
use app\service\client\AccountService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 直达链接服务
 */
abstract class SchemaService extends MediaService implements MediaInterface
{
    /**
     * 视频链接列表.
     *
     * @param array $params 请求参数
     * @param bool $isCount 是否计数
     * @return array|int
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getVideoList(array $params = [], bool $isCount = false)
    {
        $materialId = $params['material_id'] ?? '';
        $advertiserId = $params['advertiser_id'] ?? '';
        $pageSize = ! empty($params['page_size']) ? $params['page_size'] : 20;
        if (! $materialId || ! $advertiserId) {
            return $isCount ? 0 : [];
        }

        $info = (new KsAdvertiserModel())->where('advertiser_id', $advertiserId)->field(true)->find();
        if (! $info) {
            return $isCount ? 0 : [];
        }

        $where = [
            ['material_id', '=', $materialId],
            ['user_id', '=', $info->getAttr('user_id')],
        ];

        $query = (new MongoKsSchemaVideoModel())->where($where)->field(true);
        return $isCount ? $query->count() : $query->paginate($pageSize)->toArray();
    }

    /**
     * 图片链接列表.
     * @param array $params 请求参数
     * @param bool $isCount 是否计数
     * @return array|int
     * @throws DbException
     */
    public static function getPicList(array $params = [], bool $isCount = false)
    {
        $materialId = $params['material_id'] ?? '';
        $advertiserId = $params['advertiser_id'] ?? '';
        $pageSize = ! empty($params['page_size']) ? $params['page_size'] : 20;
        if (! $materialId || ! $advertiserId) {
            return $isCount ? 0 : [];
        }

        $where = [
            ['material_id', '=', $materialId],
            ['adv_id', '=', $advertiserId],
        ];

        $query = (new MongoKsSchemaPicModel())->where($where)->field(true);
        return $isCount ? $query->count() : $query->paginate($pageSize)->toArray();
    }
}
